import datetime

from peewee import *

database = MySQLDatabase('audi', **{'charset': 'utf8', 'sql_mode': 'PIPES_AS_CONCAT', 'use_unicode': True, 'host': '192.168.10.193', 'port': 3306, 'user': 'a', 'password': '110'})

class UnknownField(object):
    def __init__(self, *_, **__): pass

class BaseModel(Model):
    class Meta:
        database = database

    def to_dict(self, keys):
        data = {}
        for key in keys:
            value = getattr(self, key)
            if isinstance(value, datetime.datetime):
                data[key] = value.strftime("%Y-%m-%d %H:%M:%S")
            else:
                data[key] = value
        # data = {key:getattr(self,key) for key in keys}
        return data

class CarsSaDesc(BaseModel):
    brand_code = CharField(column_name='brandCode', constraints=[SQL("DEFAULT ''")])
    cid = CharField()
    createtime = DateTimeField()
    desc = CharField(constraints=[SQL("DEFAULT ''")])
    lang = CharField(null=True)
    sa = CharField()
    sa_family = CharField(constraints=[SQL("DEFAULT ''")])
    sa_family_desc = CharField(constraints=[SQL("DEFAULT ''")])

    class Meta:
        table_name = 'cars_sa_desc'
        indexes = (
            (('cid', 'sa', 'sa_family', 'brand_code', 'lang'), True),
        )
        primary_key = False

    def _to_dict(self):
        keys = ['brand_code', 'cid', 'desc', 'lang', 'sa', 'sa_family', 'sa_family_desc']
        return self.to_dict(keys)

